<template>
  <!-- 企业端 -->
  <div class="enterprise-info">
    <!-- 对话框组件 -->
    <AddEnterpriseDialog :visible.sync="addDialogVisible" @refresh="handleRefresh"/>
    <UpdateEnterpriseDialog :visible.sync="updateDialogVisible" :enterprise="selectedEnterprise" @on-success="handleUpdateSuccess"/>
    <DeleteDialog :visible.sync="deleteDialogVisible" :id="currentDeleteId" @confirm-delete="handleConfirmDelete"/>
    
    <AddRecruitDialog :visible.sync="showAddRecruitDialog" @submit="handleAddRecruitSubmit"/>
    <UpdateRecruitDialog :visible.sync="showUpdateRecruitDialog" :recruit-data="selectedRecruit" @submit="handleUpdateRecruitSubmit"/>

    <!-- 企业信息模块 -->
    <div v-if="enterpriseInfo">
      <!-- 展示企业信息 -->
      <el-card shadow="hover" style="margin-bottom: 20px;">
        <div class="header-bar">
          <h3 class="section-title">企业信息管理</h3>
          <div class="action-buttons">
            <el-button type="primary" size="small" @click="openUpdateDialog(enterpriseInfo)" class="action-button">
              <i class="el-icon-edit"></i>
              修改信息
            </el-button>
            <el-button type="danger" size="small" @click="openDeleteDialog(enterpriseInfo.id)" class="action-button">
              <i class="el-icon-delete"></i>
              删除企业
            </el-button>
          </div>
        </div>
        <el-descriptions style="margin-top: 20px;" :column="2" border>
          <el-descriptions-item label="企业名称">{{ enterpriseInfo.name }}</el-descriptions-item>
          <el-descriptions-item label="地址">{{ enterpriseInfo.address }}</el-descriptions-item>
          <el-descriptions-item label="联系人">{{ enterpriseInfo.contact }}</el-descriptions-item>
          <el-descriptions-item label="联系电话">{{ enterpriseInfo.contactPhone }}</el-descriptions-item>
          <el-descriptions-item label="企业邮箱">{{ enterpriseInfo.email }}</el-descriptions-item>
          <el-descriptions-item label="注册时间">
            <span class="create-time">{{ enterpriseInfo.createTime }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="企业简介">
            <pre class="introduction-text">{{ enterpriseInfo.introduction }}</pre>
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <!-- 职位信息模块 -->
      <el-card shadow="hover" style="margin-bottom: 20px;">
        <div class="header-bar">
          <h3 class="section-title">职位信息管理</h3>
          <el-button 
            type="primary" 
            size="small" 
            @click="openAddRecruitDialog" 
     
            class="action-button">
            <i class="el-icon-plus"></i>
            新增职位
          </el-button>
        </div>

        <!-- 如果有职位信息 -->
        <div v-if="recruitInfoList && recruitInfoList.length > 0">
          <el-table :data="recruitInfoList" border style="width: 100%; margin-top: 20px;">
            <el-table-column prop="jobName" label="职位名称" width="180"></el-table-column>
            <el-table-column prop="demand" label="专业限制" ></el-table-column>
            <el-table-column prop="jobRequire" label="职位要求" ></el-table-column>
            <el-table-column prop="minSalary" label="最低薪资" width="120"></el-table-column>
            <el-table-column prop="maxSalary" label="最高薪资" width="120"></el-table-column>
            <el-table-column prop="jobAddress" label="工作地址" width="120"></el-table-column>
            <el-table-column prop="jobBenefits" label="福利" width="120"></el-table-column>
            <el-table-column prop="jobAddress" label="工作地点" width="120"></el-table-column>

            <!-- <el-table-column prop="jobState" label="招聘状态" width="120">
              <template slot-scope="scope">
                {{ formatJobState(scope.row.jobState) }}
              </template>
            </el-table-column> -->

            <el-table-column prop="jobDescribe" label="职位描述"></el-table-column>
            <el-table-column label="操作" width="150">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="editRecruit(scope.row)">编辑</el-button>
                <el-button type="text" size="small" @click="deleteRecruit(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 如果没有职位信息 -->
        <el-empty v-else description="暂无职位信息">
        </el-empty>
      </el-card>
    </div>

    <!-- 如果没有企业信息 -->
    <div v-else>
      <el-empty description="暂无企业信息">
        <el-button 
          type="primary"
          @click="openAddDialog"
          class="add-enterprise-btn">
          <i class="el-icon-office-building"></i>
          新建企业信息
        </el-button>
      </el-empty>
    </div>
  </div>
</template>



<script>
import { getOwnInfo, del, addJob,updateJob,deleteJob } from '@/api/enterprise';

import AddEnterpriseDialog from '../module/AddEnterpriseDialog.vue';
import UpdateEnterpriseDialog from '../module/UpdateEnterpriseDialog.vue';
import DeleteDialog from '../module/DeleteDialog.vue';

import AddRecruitDialog from '../module/AddRecruitDialog.vue';
import UpdateRecruitDialog from '../module/UpdateRecruitDialog.vue';

export default {
  components: {
    AddEnterpriseDialog,
    UpdateEnterpriseDialog,
    DeleteDialog,

    AddRecruitDialog,
    UpdateRecruitDialog
  },
  data() {
    return {
      enterpriseInfo: null, // 当前企业的信息对象
      recruitInfoList: [], // 当前企业的职位信息列表
      addDialogVisible: false, // 控制新增对话框的显示状态
      updateDialogVisible: false, // 控制修改对话框的显示状态
      selectedEnterprise: null, // 当前选中的企业对象
      deleteDialogVisible: false, // 控制删除对话框的显示状态
      currentDeleteId: 0, // 当前待删除的企业 ID
      showAddRecruitDialog: false, // 控制新增职位对话框的显示状态
      isAddButtonDisabled: false, // 新增按钮是否禁用

      showUpdateRecruitDialog: false, // 控制修改职位对话框的显示状态
      selectedRecruit: null, // 当前选中的职位对象
    };
  },

  methods: {
    // 获取企业数据
    getData(userId) {
      getOwnInfo(userId).then((response) => {
        if (response.code === 200) {
          const data = response.data;
          if (data) {
            this.enterpriseInfo = data; // 设置企业信息
            this.recruitInfoList = data.recruitDomainList || [];
            this.isAddButtonDisabled = true; // 禁用新增企业按钮
          } else {
            this.enterpriseInfo = null; // 清空企业信息
            this.isAddButtonDisabled = false; // 启用新增企业按钮
          }
        } else {
          this.$message({
            showClose: true,
            message: '数据加载失败，请稍后再试！',
            type: 'error',
          });
        }
      }).catch((error) => {
        this.$message({
          showClose: true,
          message: '网络错误，请检查您的网络连接！',
          type: 'error',
        });
      });
    },

    // 格式化职位状态
    formatJobState(cellValue) {
      const status = Number(cellValue);
      switch (status) {
        case 1:
          return "在招";
        case 2:
          return "停止招聘";
        default:
          return "停止招聘";
      }
    },

    // 打开新增企业对话框
    openAddDialog() {
      this.addDialogVisible = true;
    },

    // 刷新数据
    handleRefresh() {
      const userId = this.$store.state.user.id;
      if (!userId) {
        this.$message.warning('请先登录后再访问此页面');
        this.$router.push('/login');
        return;
      }
      this.getData(userId);
    },

    // 打开修改对话框
    openUpdateDialog(enterprise) {
      if (!enterprise) {
        this.$message.warning("请选择需要修改的企业！");
        return;
      }
      this.selectedEnterprise = { ...enterprise }; // 深拷贝企业对象
      this.updateDialogVisible = true; // 显示修改对话框
    },

    // 处理修改成功后的逻辑
    handleUpdateSuccess() {
      this.updateDialogVisible = false; // 关闭修改对话框
      this.$message({
        message: "修改企业成功！",
        type: "success",
      });
      this.getData(this.$store.state.user.id); // 刷新企业信息
    },

    // 打开删除对话框
    openDeleteDialog(id) {
      this.currentDeleteId = id; // 设置当前待删除的企业 ID
      this.deleteDialogVisible = true; // 显示删除对话框
    },

    // 确认删除企业
    handleConfirmDelete(id) {
      del(id)
        .then((response) => {
          if (response.code === 200) {
            this.$message({
              message: "删除成功！",
              type: "success",
            });
            this.getData(this.$store.state.user.id); // 刷新企业信息
          } else {
            this.$message({
              message: "删除失败，请稍后再试！",
              type: "error",
            });
          }
        })
        .catch((error) => {
          this.$message({
            message: "网络错误，请检查您的网络连接！",
            type: "error",
          });
        });
    },

    // 打开新增职位对话框
    openAddRecruitDialog() {
      this.showAddRecruitDialog = true;
    },

    // 提交新增职位
    handleAddRecruitSubmit(formData) {
      const enterpriseId = this.enterpriseInfo?.id;
      if (!enterpriseId) {
        this.$message.warning("无法获取企业 ID，请先完善企业信息！");
        return;
      }
      const requestData = {
        ...formData,
        companyId: enterpriseId, // 将企业 ID 添加到请求参数中
      };
      
      addJob(requestData)
        .then((response) => {
          if (response.code === 200) {
            this.$message({
              message: "新增职位成功！",
              type: "success",
            });
            this.showAddRecruitDialog = false; // 关闭对话框
            this.getData(this.$store.state.user.id); // 刷新数据
          } else {
            this.$message({
              message: "新增职位失败，请稍后再试！",
              type: "error",
            });
          }
        })
        .catch((error) => {
          this.$message({
            message: "网络错误，请检查您的网络连接！",
            type: "error",
          });
        });
    },

    // 打开修改职位对话框
    editRecruit(row) {
      if (!row) {
        this.$message.warning("请选择需要修改的职位！");
        return;
      }
      this.selectedRecruit = { ...row }; // 深拷贝职位对象
      this.showUpdateRecruitDialog = true; // 显示修改对话框
    },

    // 提交修改后的职位数据
    handleUpdateRecruitSubmit(formData) {
      const enterpriseId = this.enterpriseInfo?.id;
      if (!enterpriseId) {
        this.$message.warning("无法获取企业 ID，请先完善企业信息！");
        return;
      }
      const requestData = {
        ...formData,
        companyId: enterpriseId, // 将企业 ID 添加到请求参数中
      };
      // 调用后端接口更新职位信息
      updateJob(requestData)
        .then((response) => {
          if (response.code === 200) {
            this.$message({
              message: "修改职位成功！",
              type: "success",
            });
            this.getData(this.$store.state.user.id); // 刷新数据
          } else {
            this.$message({
              message: "修改职位失败，请稍后再试！",
              type: "error",
            });
          }
        })
        .catch((error) => {
          this.$message({
            message: "网络错误，请检查您的网络连接！",
            type: "error",
          });
        });
    },

    // 删除职位
    deleteRecruit(id) {
      if (!id) {
        this.$message.warning("无法获取职位 ID，请稍后再试！");
        return;
      }

      // 弹出确认框
      this.$confirm("确定要删除该职位吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 调用后端接口删除职位
          deleteJob(id)
            .then((response) => {
              if (response.code === 200) {
                this.$message({
                  message: "删除成功！",
                  type: "success",
                });
                this.getData(this.$store.state.user.id); // 刷新数据
              } else {
                this.$message({
                  message: "删除失败，请稍后再试！",
                  type: "error",
                });
              }
            })
            .catch((error) => {
              this.$message({
                message: "网络错误，请检查您的网络连接！",
                type: "error",
              });
            });
        })
        .catch(() => {
          // 用户取消删除
          this.$message.info("已取消删除");
        });
    }

  },

  created() {
    const userId = this.$store.state.user.id;
    if (!userId) {
      this.$message.warning('请先登录后再访问此页面');
      this.$router.push('/login');
      return;
    }
    this.getData(userId);
  },
};
</script>






<style scoped>

</style>